class SocketClient {
    constructor(socketUrl, onReceive, onErrorEvent, onErrorSucceed, group_id = null) {
        this.url = socketUrl;
        this.onReFn = onReceive;
        this.onErrFn = onErrorEvent;
        this.onSucFn = onErrorSucceed;
        this.isSocketClose = false;
        this.reconnectCount = 0;
        this.heartbeatInterval = null;
        this.socketTask = null;
        this.againTimer = null;
        this.group_id = group_id || "";

        if (this.socketTask) {
            this.socketTask.close();
            this.socketTask = null;
            clearInterval(this.heartbeatInterval);
        }

        this.socketTask = new WebSocket(this.url);

        this.socketTask.onopen = (event) => {
            clearInterval(this.againTimer);
            onErrorSucceed({ isShow: false });
            this.heartbeatInterval = setInterval(() => {
                this.sendMsg({
                    contentType: "heartbeat",
                    msg: "ok",
                    content: "none"
                });
            }, 1000 * 30);
        };

        this.socketTask.onerror = (err) => {
            clearInterval(this.heartbeatInterval);
            if (!this.isSocketClose) {
                this.reconnect(this.url, this.onErrFn);
            }
        };

        this.socketTask.onclose = (e) => {
            clearInterval(this.heartbeatInterval);
            if (!this.isSocketClose) {
                this.reconnect(this.url, this.onErrFn);
            }
        };

        this.socketTask.onmessage = (res) => {
            let serverData = res.data;
            if (serverData) {
                this.onReFn(serverData);
            }
        };
    }

    reconnect(url, onErrorEvent) {
        clearInterval(this.againTimer);
        clearInterval(this.heartbeatInterval);
        if (this.socketTask) {
            this.socketTask.close();
            this.socketTask = null;
        }
        onErrorEvent({
            isShow: true,
            message: '重连中，请稍后...'
        });

        if (this.reconnectCount <= 0) {
            console.log("已超出重连次数");
            return;
        }

        this.againTimer = setInterval(() => {
            let skt = new SocketClient(this.url, this.onReFn, this.onErrFn, this.onSucFn, this.group_id);
            this.reconnectCount--;
            skt.reconnectCount = this.reconnectCount;
            clearInterval(this.againTimer);
        }, 1000 * 5);
    }

    sendMsg(msg) {
        msg = JSON.stringify(msg);
        try {
            this.socketTask.send(msg);
        } catch (e) {
            if (this.isSocketClose) {
                return;
            } else {
                this.reconnect(this.url, this.onErrFn);
            }
        }
    }

    stop() {
        this.isSocketClose = true;
        clearInterval(this.heartbeatInterval);
        clearInterval(this.againTimer);
        if (this.socketTask) {
            this.socketTask.close();
            this.socketTask = null;
        }
    }
}

export {
    SocketClient
} 